package com.example.barcodeapp.ui.own.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.barcodeapp.R;
import com.example.barcodeapp.base.BaseFragment;
import com.example.barcodeapp.bean.ownbean.XiLieKeBean;
import com.example.barcodeapp.common.Constants;
import com.example.barcodeapp.interfaces.IBasePersenter;
import com.example.barcodeapp.interfaces.own.IOwn;
import com.example.barcodeapp.persenter.OnePresenterwodekechenglanmu;
import com.example.barcodeapp.persenter.own.OnePresenter;
import com.example.barcodeapp.ui.home.AdapterRili;
import com.example.barcodeapp.ui.own.bean.LanMuBean;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

// FragmentSuYang
public class OwnFragment extends BaseFragment<IOwn.Persenterwodekechenglanmu> implements IOwn.Viewwodekecheng{
    @BindView(R.id.tl_class)
    TabLayout tlClass;
    @BindView(R.id.vp_class)
    ViewPager vpClass;




    @Override
    protected int getLayout() {
        return R.layout.fragment_own;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected OnePresenterwodekechenglanmu initPersenter() {
        return new OnePresenterwodekechenglanmu();
    }
    @Override
    protected void initData() {

        persenter.getwodekechenglanmu();
    }


    @Override
    public void getwodekechenglanmu(LanMuBean xiLieKeBean) {
        List<LanMuBean.DataEntity> dataEntities;
        dataEntities=xiLieKeBean.getData();
        ArrayList<Integer> ints = new ArrayList<Integer>();
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i <dataEntities.size() ; i++) {
            ints.add(dataEntities.get(i).getId());
            strings.add(dataEntities.get(i).getLanmu());
        }
        Log.e("asdwadasd", "run: "+strings.toString() );
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < dataEntities.size(); i++) {
            fragments.add(new FragmentSuYang());
        }

        vpClass.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        tlClass.setupWithViewPager(vpClass);
        tlClass.setSelectedTabIndicatorHeight(0);
        tlClass.getTabAt(0).setText(strings.get(0)).isSelected();
        for (int i = 1; i < strings.size(); i++) {
            tlClass.getTabAt(i).setText(strings.get(i));

        }


        View root = LayoutInflater.from(getActivity()).inflate(R.layout.cch_tab_text_item, null);
        TextView tv_name = root.findViewById(R.id.tv_name);
        tv_name.setTextSize(20);
        tv_name.setText("音乐素养");
        tlClass.getTabAt(0).setCustomView(root);
        tlClass.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View root = LayoutInflater.from(getActivity()).inflate(R.layout.cch_tab_text_item, null);
                TextView tv_name = root.findViewById(R.id.tv_name);
                tv_name.setTextSize(20);
                tv_name.setText(tab.getText());
                tab.setCustomView(root);
                Constants.lanmuid=ints.get(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}